package com.hckj.jianzhijia;

import android.app.Application;
import android.content.Context;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.GetCallback;
import com.avoscloud.leanchatlib.controller.ChatManager;
import com.avoscloud.leanchatlib.utils.ThirdPartUserUtils;
import com.baidu.mapapi.SDKInitializer;
import com.hckj.jianzhijia.chat.CustomUserProvider;
import com.hckj.jianzhijia.interfaces.ListFreshInterface;
import com.hckj.jianzhijia.interfaces.UserInfoChangeInterface;
import com.hckj.jianzhijia.model.CompanyUser;
import com.hckj.jianzhijia.model.VersionUpdate;
import com.hckj.jianzhijia.utils.CatchErrorHandler;
import com.hckj.jianzhijia.utils.Data;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.ArrayList;

/**
 * Created by 赵奇隆 on 2015-10-18.
 * 如果出現方法超過65535問題解決：http://blog.csdn.net/t12x3456/article/details/40837287
 * 盡量少用靜態數據，公共數據的存储使用Applacation进行存储
 * 由于android中java虚拟机不完善，使用static会垃圾回收导致出现空指针，所有数据存入这里
 * http://blog.csdn.net/lovexieyuan520/article/details/46289215
 */
public class JianzhijiaApplacation extends Application {

    private CompanyUser companyUser = null;
    private ArrayList<UserInfoChangeInterface> userInfoChangeInterfaceArrayList;
    private ArrayList<ListFreshInterface> jobListFreshInterfaces;

    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                                                                                          context)
                                                      .threadPoolSize(3).threadPriority(Thread.NORM_PRIORITY - 2)
                                                                                     //.memoryCache(new WeakMemoryCache())
                                                      .denyCacheImageMultipleSizesInMemory()
                                                      .tasksProcessingOrder(QueueProcessingType.LIFO)
                                                      .build();
        ImageLoader.getInstance().init(config);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        AVOSCloud.initialize(getApplicationContext(), Data.appId, Data.appKey);
        userInfoChangeInterfaceArrayList = new ArrayList<UserInfoChangeInterface>();
        SDKInitializer.initialize(getApplicationContext());
        jobListFreshInterfaces = new ArrayList<ListFreshInterface>();
        CatchErrorHandler catchErrorHandler = CatchErrorHandler.getInstance();
        catchErrorHandler.init(this);
        ChatManager.setDebugEnabled(true);// tag leanchatlib
        AVOSCloud.setDebugLogEnabled(true);  // set false when release
        ChatManager.getInstance().init(this);
        initImageLoader(this);
        ThirdPartUserUtils.setThirdPartUserProvider(new CustomUserProvider());
    }

    public CompanyUser getCompanyUser() {
        return companyUser;
    }

    public void setCompanyUser(CompanyUser companyUser) {
        this.companyUser = companyUser;
    }

    public void addUserInfoChange(UserInfoChangeInterface userInfoChangeInterface) {
        userInfoChangeInterfaceArrayList.add(userInfoChangeInterface);
    }

    public void updateCompanyInfo() {
        for (UserInfoChangeInterface u : userInfoChangeInterfaceArrayList) {
            u.updateCompanyInfo();
        }
    }

    public void updateCompanyLogo() {
        for (UserInfoChangeInterface u : userInfoChangeInterfaceArrayList) {
            u.updateCompanyLogo();
        }
    }

    public void removeUserInfoChangeInterface(UserInfoChangeInterface u) {
        userInfoChangeInterfaceArrayList.remove(userInfoChangeInterfaceArrayList.indexOf(u));
    }

    public void addJobListFreshInterfaces(ListFreshInterface listFreshInterface) {
        jobListFreshInterfaces.add(listFreshInterface);
    }

    public void removeJobListFreshInterface(ListFreshInterface listFreshInterface) {
        jobListFreshInterfaces.remove(jobListFreshInterfaces.indexOf(listFreshInterface));
    }

    public void updateJobListFresh() {
        for (ListFreshInterface u : jobListFreshInterfaces) {
            u.afterUpdateListFresh();
        }
    }

}
